package com.wlr.blog.web.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wlr.blog.web.common.entity.Album;
import com.wlr.blog.web.common.entity.ImgInfo;
import com.wlr.blog.web.common.mapper.AlbumMapper;
import com.wlr.blog.web.common.mapper.ImgInfoMapper;
import com.wlr.blog.web.common.service.AlbumService;
import com.wlr.blog.web.common.vo.AlbumVO;
import com.wlr.blog.web.common.vo.ImgInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 相册服务
 */
@Service
public class AlbumServiceImpl extends ServiceImpl<AlbumMapper, Album>
        implements AlbumService {

    @Autowired
    AlbumMapper albumMapper;
    @Autowired
    ImgInfoMapper imgInfoMapper;

    /**
     * 获取相册列表
     *
     * @return 相册列表
     */
    @Override
    public List<AlbumVO> getAlbumVoList() {
        List<Album> albums = albumMapper.selectList(Wrappers.emptyWrapper());
        List<AlbumVO> result = albums.stream().map(a -> {
            AlbumVO albumVO = new AlbumVO();
            BeanUtils.copyProperties(a, albumVO);
            albumVO.setImgInfoList(imgInfoMapper.selectList(new QueryWrapper<ImgInfo>().eq("album_id", albumVO.getId())));
            return albumVO;
        }).collect(Collectors.toList());
        return result;
    }

    /**
     * 获取相册内的图片根据编号
     *
     * @param id 相册编号
     * @return 相册内的图片
     */
    @Override
    public ImgInfoVO getPhotoById(Integer id) {
        Album album = albumMapper.selectById(id);
        ImgInfoVO result = new ImgInfoVO();
        result.setPhotoAlbumCover(album.getAlbumCover());
        result.setPhotoAlbumName(album.getAlbumDesc());
        List<ImgInfo> imgInfoList = imgInfoMapper.selectList(new QueryWrapper<ImgInfo>().eq("album_id", id));
        result.setImgInfoList(imgInfoList);
        return result;
    }
}
